package com.itic.hpp.user;

import com.itic.appbase.framework.Base.BaseCode;
import com.itic.mobile.base.BaseMobile;
import com.itic.system.user.address.persistence.model.UserAddressExample;
import com.itic.system.user.address.service.UserAddressService;
import com.itic.system.vo.UserAddressVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/hpp/user/address")
public class UserAddressInterface extends BaseMobile {
    @Autowired
    private UserAddressService userAddressService;

    /**
     * 添加或者编辑收货地址
     * @param addressId
     * @param userId
     * @param regionId
     * @param reciveName
     * @param recivePhone
     * @param zipCode
     * @param address
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    @ResponseBody
    public Map<String, Object> addOrEdit(String addressId,String userId,String regionId,String reciveName,String recivePhone,String zipCode,String address, HttpServletRequest request,
                                   HttpServletResponse response) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            //效验参数userId 省市区Id 收货人电话 名称 邮编 收货地址Id
            if(StringUtils.isNoneBlank(userId,regionId,reciveName,recivePhone,address)){
                resultMap = this.userAddressService.addOrEdit(addressId,userId,regionId,reciveName,recivePhone,zipCode,address);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        } catch (Exception e) {
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("UserAddressInterface", "addOrEdit", e.toString(), "添加或者编辑收货地址", e);
        }
        return resultMap;
    }

    /**
     * 收货地址列表 (分页)
     * @param userId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(String userId,HttpServletRequest request,
                                         HttpServletResponse response) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            //效验参数userId
            if(StringUtils.isNoneBlank(userId)){
                //查看用户收货地址
                UserAddressExample example = new UserAddressExample();
                this.handlePageQueryDefault(request,example);
                List<Map<String, Object>> list = this.userAddressService.findUserAddress(example,userId);
                resultMap.put(BaseCode.DATA, list);
                resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        } catch (Exception e) {
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("UserAddressInterface", "list", e.toString(), "收货地址列表", e);
        }
        return resultMap;
    }

    /**
     * 删除用户收货地址
     * @param userId
     * @param addressId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, Object> del(String userId,String addressId,HttpServletRequest request,HttpServletResponse response) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            //效验参数userId
            if(StringUtils.isNoneBlank(userId,addressId)){
                //删除收货地址
                resultMap = this.userAddressService.delUserAddress(userId,addressId);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        } catch (Exception e) {
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("UserAddressInterface", "list", e.toString(), "删除地址列表", e);
        }
        return resultMap;
    }

    /**
     * 地址详情
     * @param addressId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Map<String, Object> detail(String addressId,HttpServletRequest request,HttpServletResponse response) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            //效验参数userId
            if(StringUtils.isNoneBlank(addressId)){
                //删除收货地址
                UserAddressVo userAddressVo= this.userAddressService.getAddressDetail(addressId);
                resultMap.put(BaseCode.DATA, userAddressVo);
                resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        } catch (Exception e) {
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("UserAddressInterface", "detail", e.toString(), "地址详情列表", e);
        }
        return resultMap;
    }


}